<?php

namespace app\modules\mall\controllers;

use Yii;
use app\controllers\ApiController;
use app\modules\mall\models\Goods;
use app\modules\mall\models\Group;
class GroupController extends ApiController
{
    
    const DEFAULT_SIZE = 2;
    const FLASH_TYPE = 2;
    
    /**
     * 团购商品列表
     * GET
     */
    public function actionList() {
        $data = [];
        $sorts = trim(Yii::$app->request->get('sorts'));
        $data['page'] = $this->_getPage();
        $data['size'] = 10;
        
        $goodsModel = new Goods();
        $groupModel = new Group();
    
        $list = $groupModel->getGoodslist($data);
    
        //得到商品的商家名
        foreach($list as $key=>$value) {
            $list[$key]['shopname'] = $groupModel->getShopName($value['sid']);
            //是否包邮: 1-买家承担运费 2:卖家承担运费
            $list[$key]['isShippingFree'] = $goodsModel->isGoodsLogisticsFree($value['tpl_id']);
        }
        $this->_exit(0, $list);
    }
    
    /**
     * 团购商品详情
     * GET
     */
    public function actionGoodsdetail() {
        $goodsid    = $this->_validateNum(trim(Yii::$app->request->get('goodsid')));
        
        $groupModel = new Group();
        $goodsModel = new Goods();
        
        $info = $groupModel->getGoodsInfo($goodsid);
        if(empty($info)) {
            $this->_exit(40010);
        }
        //信息数
        $info['hasMsg'] = '1';
        
        //商店信息
        $shopInfo = $this->cget(Yii::$app->params['mall_domain'].'/mall/order/getshopinfo?sid=' . $info['sid']);
        $info['shopname'] = $shopInfo['shop_name'];
        $info['shopContent'] = $shopInfo['content'];
        
        $info['isShippingFree'] = $this->cget(Yii::$app->params['mall_domain'].'/mall/order/isshopfree?goodsid=' . $goodsid);
        
        //商品最大价，最小价
        $info['maxPrice'] = $groupModel->getGoodsMaxPrice($goodsid);
        $info['minPrice'] = $groupModel->getGoodsMinPrice($goodsid);
        $info['location'] = $this->ip2Location();
        //查看商品评论信息
        $tmp = [];
        $tmp['goods_id'] = $goodsid;
        $tmp['type'] = self::FLASH_TYPE;
        $tmp['page'] = 1;
        $tmp['size'] = 1;
        $comment = $goodsModel->getCommentList($tmp);
        if(!empty($comment)) {
            $uid = $comment[0]['uid'];
            $user = $this->cget(Yii::$app->params['passport_domain'].'ucenter/finduserinformation?uid='.$uid);
            $user = json_decode($user,true);
            $comment[0]['avatar'] = $user['data']['avatar'];
            $comment[0]['username'] = $user['data']['nickname'];
            $info['comment'] = $comment[0];
        } else {
            $info['comment'] = [];
        }
        
        //商品是否收藏
        if($this->isLogin()) {
            $uid = $this->_getUid();
            $tmp = [];
            $tmp['uid'] = $uid;
            $tmp['goodsid'] = $goodsid;
            $tmp['type'] = self::FLASH_TYPE;
            $info['isCollect'] = $goodsModel->isCollect($tmp);
        } else {
            $info['isCollect'] = 0;
        }
        
        $sku = $groupModel->hasSku($goodsid);
        
        $arr = [];
        if($sku) {
            $colors = $groupModel->getSkucolor($goodsid);
            $arr['color'] = [];
            foreach($colors as $key=>$value) {
                $arr['color'][$key]['color'] = $value['color'];
                $arr['color'][$key]['exist'] = $groupModel->existColor($goodsid, $value['color']);
            }
        
            $sizes = $groupModel->getSkusize($goodsid);
            $arr['size'] = [];
            foreach($sizes as $key=>$value) {
                $arr['size'][$key]['size'] = $value['size'];
                $arr['size'][$key]['exist'] = $groupModel->existSize($goodsid, $value['size']);
            }
            $arr['skulist'] = $groupModel->getSkulist($goodsid);
        }
        $info['skulist'] = $arr;
        
        if(empty($info)) {
            $this->_exit(40010);
        } else {
            $this->_exit(0, $info);
        }
    }
    
}
